package com.k.kc.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author kivil
 */
public class LicenseUtils {
    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(LicenseUtils.class);
    private static final String[] LICENSE_UPLOAD_DIR_ARR = new String[]{"usr","CommandCenter","dss","ConfigFiles","Server"};
    private static final String LICENSE_UPLOAD_FILENAME = "ConfigL.dat";

    private static final String ENCRYPT_TYPE = "AES";

    private static final String ENCRYPT_MODEL = "AES/ECB/NoPadding";

    private static final byte[] KEY_BYTE_ARR = {
            0x76, 0x12, 0x6D, 0x3E,
            0x64, 0x5C, 0x4B, 0x35,
            0x08, 0x17, 0x27, 0x2F,
            0x5E, 0x2A, 0x49, 0x57
    };

    private static final int DISPATCHER_START_INDEX = 12;

    private static final int TRAIN_START_INDEX = 56;

    public static LicenseVo parseLicense(String filePathname) throws Exception {
        byte[] bytesSrc = FileUtils.readFileToByteArray(filePathname);
        SecretKeySpec skeySpec = new SecretKeySpec(KEY_BYTE_ARR, ENCRYPT_TYPE);
        Cipher cipher = Cipher.getInstance(ENCRYPT_MODEL);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] original = cipher.doFinal(bytesSrc);
        int dispatcherCount = original[DISPATCHER_START_INDEX] & 0xff | ((original[DISPATCHER_START_INDEX + 1] & 0xff )<< 8);
        int dispatcherCountCheck = original[DISPATCHER_START_INDEX + 2] & 0xff | ((original[DISPATCHER_START_INDEX + 3] & 0xff )<< 8);
        int trainCount = original[TRAIN_START_INDEX] & 0xff | ((original[TRAIN_START_INDEX + 1] & 0xff )<< 8);
        int trainCountCheck = original[TRAIN_START_INDEX + 2] & 0xff | ((original[TRAIN_START_INDEX + 3] & 0xff )<< 8);
        LOGGER.warn("license Decrypt dispatcherCount:{},dispatcherCountCheck:{},trainCount:{},trainCountCheck:{}",dispatcherCount,dispatcherCountCheck,trainCount,trainCountCheck);
        return LicenseVo.builder().dispatcherCount(String.valueOf(dispatcherCount))
                .dispatcherCountCheck(String.valueOf(dispatcherCountCheck))
                .trainCount(String.valueOf(trainCount))
                .trainCountCheck(String.valueOf(trainCountCheck))
                .build();
    }

    public static String checkLicense(int count) {
        String ret = null;
        String filePathname = FileUtils.getFilePath(FileUtils.formatFileDir(LICENSE_UPLOAD_DIR_ARR),LICENSE_UPLOAD_FILENAME);
        LicenseVo licenseVo;
        try {
            licenseVo = parseLicense(filePathname);
            String trainCount = licenseVo.getTrainCount();
            Integer trainCountInt = Integer.valueOf(trainCount);
            LOGGER.warn("excel count:{}, license count:{}",count,trainCountInt);
            if(count > trainCountInt){
                ret = "License 数量限制";
            }
        } catch (Exception e) {
            ret = "License 无效或不存在";
            e.printStackTrace();
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println((1& 0xff) | 2& 0xff);
    }
}
